package com.zzs.server.controller;

import com.zzs.common.core.enums.ResponseCode;
import com.zzs.common.core.response.Result;
import com.zzs.common.core.util.ThreadLocalUtil;
import com.zzs.common.security.exception.ServiceException;
import com.zzs.server.pojo.dto.LoginDTO;
import com.zzs.server.service.AdminService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zzs
 * @date 2025/03/17
 */
@RestController
@RequestMapping("/server/user")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody LoginDTO loginDTO) {
        String token = adminService.login(loginDTO.getAccount().trim(), loginDTO.getPassword().trim());
        return Result.success(token);
    }

    @DeleteMapping("/logout")
    public Result<Void> logout() {
        Long adminId = ThreadLocalUtil.getUserId();
        if (adminId == null) {
            throw new ServiceException(ResponseCode.UNAUTHORIZED);
        }
        adminService.logout(adminId);
        return Result.success();
    }
}
